package com.ruoyi.vod.controller;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.vod.service.VodService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 阿里云视频点播微服务
 *
 * @author Alex
 * @version 1.0
 * @date 2023/9/16 19:14
 */
@RestController
@RequestMapping("/vod")
public class VodController {

    @Resource
    private VodService vodService;

    /**
     * 上传视频到阿里云
     *
     * @param file 视频文件
     * @return 视频播放id
     */
    @PostMapping("/upload")
    public AjaxResult uploadVideo(MultipartFile file) {
        String videoId = vodService.uploadVideo(file);
        return videoId == null ? AjaxResult.error("上传视频失败") : AjaxResult.success(videoId);
    }

    /**
     * 删除上传的视频
     *
     * @param id 视频id
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public AjaxResult removeVideo(@PathVariable String id) {
        return vodService.removeVideo(id) ? AjaxResult.success("删除视频成功") : AjaxResult.error("删除视频失败");
    }

    /**
     * 根据视频id获取视频凭证
     *
     * @param courseId        课程id，方便记录课程学习次数
     * @param videoResourceId 视频id
     * @return 播放凭证
     */
    @GetMapping("/{courseId}/{videoResourceId}")
    public AjaxResult getPlayAuth(@PathVariable String courseId, @PathVariable String videoResourceId) {
        String authCode = vodService.getPlayAuth(courseId, videoResourceId);
        return authCode == null ? AjaxResult.error("获取视频播放凭证失败") : AjaxResult.success(authCode);
    }

}
